package cn.taoshop.utils;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

// qq邮件发送工具类
 
public class MailUtil {
	/*
	 * to：收件人
	 * subject：主题
	 * emailContent：邮件内容
	 */
	public static void sendMail(String to,String subject,String emailContent){
		
		// 1.获得连接对象
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.smtp.host", "smtp.qq.com");
        props.setProperty("mail.smtp.auth", "true");
        props.put("mail.smtp.port","587");
        props.setProperty("mail.smtp.ssl",  "true");  
        props.setProperty("mail.debug", "true");
		Session session = Session.getInstance(props, new Authenticator() {

		@Override
		protected PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication("919913875@qq.com", "dznezopawusubcad");
		}
		});
		// 2.创建邮件对象:
		Message message = new MimeMessage(session);
		// 设置发件人:
		try {
			message.setFrom(new InternetAddress("919913875@qq.com"));
			// 设置收件人:
			message.addRecipient(RecipientType.TO, new InternetAddress(to));
			// 设置标题
			message.setSubject(subject);
			// 设置邮件正文:
			message.setContent(emailContent, "text/html;charset=UTF-8");
			// 3.发送邮件:
			Transport.send(message);
			
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
}
